package com.example.demo.controller;

import com.example.demo.http.HttpClient;
import com.example.demo.service.UserService;
import com.example.demo.util.MyManyThreadRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author x c
 * @Email 378661553@qq.com
 * @Date Created in 2019/12/20 11:43
 */
@RestController
public class IndexController implements EnvironmentAware {

    private Environment environment;

    @Autowired
    UserService userService;

    @GetMapping("/index")
    public Object index(HttpServletRequest request) {
        return System.currentTimeMillis();
    }

    @GetMapping("/sql")
    public Object sql(HttpServletRequest request) {
        return userService.sql(System.currentTimeMillis());
    }

    public static void main(String[] args) throws IOException {
        HttpClient httpClient = new HttpClient();
        MyManyThreadRequest myManyThreadRequest = new MyManyThreadRequest(() -> {
            httpClient.doGet("http://localhost/demo/index", null);
        }, 50);

        int successTaskNum = myManyThreadRequest.getSuccessTaskNum();
        System.out.println(successTaskNum);

        System.in.read();
    }

    @PostMapping("index")
    public Object index(@RequestBody Map<String,Object> map) {
        return System.currentTimeMillis();
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
